package com.ringtheweb.common.configuration;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.configuration.Configuration;

/**
 * Adapter of {@link Configuration} to {@link IConfiguration}
 * @author ldewavri
 *
 */
public class CommonsConfigurationAdapter implements IConfiguration {
	
	private Configuration configuration;
	
	public CommonsConfigurationAdapter(Configuration configuration) {
		this.configuration = configuration;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean containsKey(String key) {
		return configuration.containsKey(key);
	}

	/**
	 * {@inheritDoc}
	 */
	public BigDecimal getBigDecimal(String key) {
		try {
			return configuration.getBigDecimal(key);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
		try {
			return configuration.getBigDecimal(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public BigInteger getBigInteger(String key) {
		try {
			return configuration.getBigInteger(key);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public BigInteger getBigInteger(String key, BigInteger defaultValue) {
		try {
			return configuration.getBigInteger(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public boolean getBoolean(String key) {
		try {
			return configuration.getBoolean(key);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public boolean getBoolean(String key, boolean defaultValue) {
		try {
			return configuration.getBoolean(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public Boolean getBoolean(String key, Boolean defaultValue) {
		try {
			return configuration.getBoolean(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public byte getByte(String key) {
		try {
			return configuration.getByte(key);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public byte getByte(String key, byte defaultValue) {
		try {
			return configuration.getByte(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public Byte getByte(String key, Byte defaultValue) {
		try {
			return configuration.getByte(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public double getDouble(String key) {
		try {
			return configuration.getDouble(key);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public double getDouble(String key, double defaultValue) {
		try {
			return configuration.getDouble(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public Double getDouble(String key, Double defaultValue) {
		try {
			return configuration.getDouble(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public float getFloat(String key) {
		try {
			return configuration.getFloat(key);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public float getFloat(String key, float defaultValue) {
		try {
			return configuration.getFloat(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public Float getFloat(String key, Float defaultValue) {
		try {
			return configuration.getFloat(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public int getInt(String key) {
		try {
			return configuration.getInt(key);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public int getInt(String key, int defaultValue) {
		try {
			return configuration.getInt(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public Integer getInteger(String key, Integer defaultValue) {
		try {
			return configuration.getInt(key,defaultValue);
		}
		catch (org.apache.commons.configuration.ConversionException e) {
			throw new ConversionException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public Iterator<?> getKeys(String prefix) {
		return configuration.getKeys(prefix);
	}

	/**
	 * {@inheritDoc}
	 */	
	public Iterator<?> getKeys() {
		return configuration.getKeys();
	}

	/**
	 * {@inheritDoc}
	 */	
	public List<?> getList(String key) {
		return configuration.getList(key);
	}

	/**
	 * {@inheritDoc}
	 */	
	public  List<?> getList(String key, List<?> defaultValue) {
		return configuration.getList(key,defaultValue);
	}

	/**
	 * {@inheritDoc}
	 */	
	public long getLong(String key) {
		return configuration.getLong(key);
	}

	/**
	 * {@inheritDoc}
	 */	
	public long getLong(String key, long defaultValue) {
		return configuration.getLong(key,defaultValue);
	}

	/**
	 * {@inheritDoc}
	 */	
	public Long getLong(String key, Long defaultValue) {
		return configuration.getLong(key,defaultValue);
	}

	/**
	 * {@inheritDoc}
	 */	
	public Properties getProperties(String key) {
		return configuration.getProperties(key);
	}

	/**
	 * {@inheritDoc}
	 */	
	public Object getProperty(String key) {
		return configuration.getProperty(key);
	}

	/**
	 * {@inheritDoc}
	 */	
	public short getShort(String key) {
		return configuration.getShort(key);
	}

	/**
	 * {@inheritDoc}
	 */	
	public short getShort(String key, short defaultValue) {
		return configuration.getShort(key,defaultValue);
	}

	/**
	 * {@inheritDoc}
	 */	
	public Short getShort(String key, Short defaultValue) {
		return configuration.getShort(key,defaultValue);
	}

	/**
	 * {@inheritDoc}
	 */	
	public String getString(String key) {
		return configuration.getString(key);
	}

	/**
	 * {@inheritDoc}
	 */	
	public String getString(String key, String defaultValue) {
		return configuration.getString(key,defaultValue);
	}

	/**
	 * {@inheritDoc}
	 */	
	public String[] getStringArray(String key) {
		return configuration.getStringArray(key);
	}

	/**
	 * {@inheritDoc}
	 */	
	@SuppressWarnings("unchecked")
	public Map<String, String> getAllAsString() {
		Map<String,String> configMap = new HashMap<String, String>();
		
		Iterator it = getKeys();
		String key,value = null;
		while (it.hasNext()) {
			try {
				 key = (String)it.next();
				 value = getString(key);
				 configMap.put(key, value);
			}
			catch(org.apache.commons.configuration.ConversionException e) {
				// �choue en silence
			}

		}
		
		return configMap;
	}
	

}
